package com.gdut.demo.controller;

import com.gdut.demo.entity.Ecg;
import com.gdut.demo.service.EcgService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;

@Controller
public class EcgController {

    private int count = 1;

    @Autowired
    private EcgService ecgService;

    static Integer idd = 1;

    /*修改默认启动页面*/
    @RequestMapping("/")
    public String index() {
//        return "testGetAll";
//        return "testGetAll002";
        return "redirect:/multiChannel";
    }

    @GetMapping(value = "/multiChannel")
    public String multiChannel(HttpServletRequest request,
                               Model model){

        JSONArray fWaveJsonArray = new JSONArray();
        List<Ecg> fWave = ecgService.getFWave();
        for (int i = 0; i < fWave.size(); i++){
            HashMap fWaveHashMap = new HashMap<String, String>();
            fWaveHashMap.put("id", fWave.get(i).getId());
            fWaveHashMap.put("voltage", fWave.get(i).getVoltage());
            fWaveJsonArray.add(fWaveHashMap);
        }
        //System.out.println(fWaveJsonArray);

        JSONArray mixJsonArray = new JSONArray();
        List<Ecg> mix = ecgService.getMix();
        for (int j = 0; j < mix.size(); j++){
            HashMap mixHashMap = new HashMap<String, String>();
            mixHashMap.put("id", mix.get(j).getId());
            mixHashMap.put("voltage", mix.get(j).getVoltage());
            mixJsonArray.add(mixHashMap);
        }


        JSONArray ecgJsonArray = new JSONArray();
        List<Ecg> ecg = ecgService.getEcg();
        for (int q = 0; q < ecg.size(); q++){
            HashMap ecgHashMap = new HashMap<String, String>();
            ecgHashMap.put("id", ecg.get(q).getId());
            ecgHashMap.put("voltage", ecg.get(q).getVoltage());
            ecgJsonArray.add(ecgHashMap);
        }

        request.setAttribute("fWaveJsonArray",fWaveJsonArray);
        request.setAttribute("mixJsonArray",mixJsonArray);
        request.setAttribute("ecgJsonArray",ecgJsonArray);


        //model.addAttribute("fWaveJsonArray",fWaveJsonArray);
        return "multiChannel";
    }


    /*修改默认启动页面*/
    @PostMapping("/sendEcg")
    @ResponseBody
    public String sendEcg(@RequestBody String data) {
//        System.out.println("=========================");
//        System.out.println(count);
//        count++;
        JSONArray jsonArray = JSONArray.parseArray(data);

//        JSONObject jb = JSONObject.parseObject(data);

        System.out.println(jsonArray);

//        System.out.println(jsonArray.get(0));
//        System.out.println("=========================");

//        System.out.println("接收到Ecg数据");
        return "received";
    }

    @GetMapping("/queryOneData")
    @ResponseBody
    public String getData(@RequestParam("id") String id, HttpServletRequest request){
//    public String getData(@RequestBody String id, HttpServletRequest request) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        Ecg ecg = ecgService.getOne(Integer.valueOf(id));
//        Integer ecgVoltage = ecg.getVoltage();
        System.out.println("id "+ id+"  ecgVoltage "+ecg.getVoltage());
        jsonObject.put("value",ecg.getVoltage());
        return jsonObject.toString();
    }

    @GetMapping("/queryOne")
    @ResponseBody
    public String get(HttpServletRequest request, HttpServletResponse response) {
//    public String getData(@RequestBody String id, HttpServletRequest request) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        Ecg ecg = ecgService.getOne(Integer.valueOf(idd));

//        Integer ecgVoltage = ecg.getVoltage();
        request.setAttribute("idd",idd);
        System.out.println("id "+ idd+"  ecgVoltage "+ecg.getVoltage());
        jsonObject.put("idd",idd);
        jsonObject.put("value",ecg.getVoltage());

        idd++;
        if (idd == 1001) {
            idd = 1;
        }
        return jsonObject.toString();
    }

    @GetMapping("/getAll")
    @ResponseBody
    public JSONArray getAll(HttpServletRequest request)  {

        //创建json数组
        JSONArray jsonArray = new JSONArray();

        //获取数据
        List<Ecg> ecgList = ecgService.getAll();
        System.out.println(ecgList);
        for (int i = 0; i < ecgList.size(); i++) {
            HashMap hashMap = new HashMap<String,String>();
            hashMap.put("id",ecgList.get(i).getId());
            hashMap.put("voltage",ecgList.get(i).getVoltage());
            jsonArray.add(hashMap);
        }

        System.out.println(jsonArray);

        //返回字符串
        return jsonArray;
    }
}
